#ifndef IGSLIB_GPIO_H
#define IGSLIB_GPIO_H

#include <set>
#include <igslib_igpio.h>

namespace igslib 
{
    class CGPIO:public IGPIO
    {
        protected:
            mode dir;
            state st;
            std::set<IGPIOEvent*> events;
            int number;
			ISpi* spi;
        public:
            CGPIO()
            {
                dir = mode::input;
                st = state::low;
                spi = nullptr;
            }
            virtual int SetMode(const mode& value)
            {
                mode old = dir;
                dir = value;
                if (old != value)
                {
                    for(auto& each:events)
                    {
                        each->OnChangeMode(this,old,value);
                    }
                }
            }
            virtual void SetEventHandler(IGPIOEvent* value) { events.insert(value); }
            virtual mode GetMode() { return dir; }
            virtual state GetState() { return st; }
            virtual int SetState(const state& value)
            {
                int rtn = 0;
                state old = st;
                st = value;
                if (old != value)
                {
                    for(auto& each:events)
                    {
                        each->OnChangeState(this,old,value);
                    }
                }
                return rtn;
            }
            virtual void Update() { }
            virtual std::string GetName() { return std::string("CGPIO"); }
			virtual std::string GetPinName() { return std::string("CGPIO_PinName"); }
            virtual int GetNumber() { return number; }
            virtual int SetNumber(const int& value)
            {
                number = value;
                return 0;
            }
            virtual void SetSPI(ISpi* value) { spi = value; }
    };

    class CGPIOManager:public IGPIOManager
    {
        static IGPIOManager* instance;
		char count;
        public:
            static IGPIOManager* GetInstance();
            IGPIO* NewM01GPIO(const int&);
			IGPIO* New_ProjMCU(const std::string&);
    };
};

#endif
